package com.songcha.library_business.helper


import android.app.Application
import com.songcha.library_common.util.ToastUtil
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXTextObject
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelpay.PayReq


class WxHelper {
    companion object{
        private lateinit var wxApi: IWXAPI
        const val SHARE_TYPE_SESSION = 1
        const val SHARE_TYPE_FRIEND_GROUP = 2

        @JvmStatic
        fun getWxApi(): IWXAPI {
            return wxApi
        }


        @JvmStatic
        fun initWxApi(application:Application,appId:String){
            wxApi= WXAPIFactory.createWXAPI(application, appId, true)
            wxApi.registerApp(appId)
        }


        @JvmStatic
        fun wxlogin():Boolean{
            //return false
            if(!checkWxInstalled()) return false
            val req = SendAuth.Req()
            req.scope = "snsapi_userinfo" // only can set snsapi_userinfo
            wxApi.sendReq(req)
            return true
        }

        @JvmStatic
        fun checkWxInstalled():Boolean{
            //return false
            if(!wxApi.isWXAppInstalled){
                ToastUtil.show("请先安装微信客户端")
                return false
            }
            return true
        }

        @JvmStatic
        fun shareText(content:String,type:Int):Boolean{
            //return false
            if(content.isEmpty()) return false
            if(!checkWxInstalled()) return false

            val textObj = WXTextObject()
            textObj.text = content

            val msg = WXMediaMessage()
            msg.mediaObject = textObj
            msg.description = content

            val req = SendMessageToWX.Req()
            req.message = msg
            if(type == SHARE_TYPE_SESSION){
                req.scene = SendMessageToWX.Req.WXSceneSession
            }else if(type == SHARE_TYPE_FRIEND_GROUP){
                req.scene = SendMessageToWX.Req.WXSceneTimeline
            }

            wxApi.sendReq(req)

            return true
        }

        @JvmStatic
        fun sendReq(appid:String,partnerId:String,prepayId:String,nonceStr:String,timestamp:String,sign:String,packageVal:String){
            val request = PayReq()
            request.appId = appid
            request.partnerId = partnerId
            request.prepayId = prepayId
            request.nonceStr = nonceStr
            request.timeStamp = timestamp
            request.sign = sign
            request.packageValue = packageVal
            wxApi.sendReq(request)
        }

    }
}


/*import android.app.Application

class WxHelper {
    companion object{
        //private lateinit var wxApi: IWXAPI
        const val SHARE_TYPE_SESSION = 1
        const val SHARE_TYPE_FRIEND_GROUP = 2


        @JvmStatic
        fun initWxApi(application: Application, appId:String){

        }


        @JvmStatic
        fun wxlogin():Boolean{
            return false

        }

        @JvmStatic
        fun checkWxInstalled():Boolean{
            return false
        }

        @JvmStatic
        fun shareText(content:String,type:Int):Boolean{
            return false
        }

        @JvmStatic
        fun sendReq(appid:String,partnerId:String,prepayId:String,nonceStr:String,timestamp:String,sign:String,packageVal:String){

        }

    }
}*/
